package com.wangle.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.pro.bean.Groups;
import com.dao.DBConnection;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;



public class GroupImpl {
		
	public GroupImpl(){}

	//��ѯ�ܵķ�ҳ
	private int pageCounts=0;
	//��ѯ��������Ŀ
	private int resultItems=0;
	/**
	 * ʵ�ַ�����ӹ���
	 * @throws SQLException 
	 * */
     public boolean save(Groups group){
		
		boolean flag=true;
		Connection conn = null;
		try {
			conn=DBConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		PreparedStatement stmt = null;
		try {
			//id��������Ҫʲ��Ϣ�����ʲôֵ����
			stmt=conn.prepareStatement("insert into groups(groupname,prgroupid,groupnote) values(?,?,?)");
			stmt.setString(1, group.getGroupname());
			stmt.setInt(2, group.getPrgroupid());
			stmt.setString(3, group.getGroupnote());
			
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag=false;
			e.printStackTrace();
		}/*finally{
		
		}
		*/
		return flag;
	}
	/**
	 * ɾ�����ڵ�ĳһ������
	 * @throws SQLException 
	 */
     public boolean delete(Groups group){
    	 boolean flag=true;
    	 Connection conn = null;
		try {
			conn = DBConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	 PreparedStatement stmt=null;
    	 try {
			stmt=conn.prepareStatement("delete from groups where groupid=?");
			stmt.setInt(1, group.getGroupid());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			flag=false;
			e.printStackTrace();
		}
		return flag;
     }
 	/**@author Administrator
 	 * ���·������
 	 */
 	public boolean update(Groups group){
 		boolean flag=true;
 		try {
			Connection conn=DBConnection.getConnection();
			PreparedStatement stmt=null;
			stmt=conn.prepareStatement("update groups set groupname=?,prgroupid=?,groupnote=? where groupid=?");
			stmt.setString(1, group.getGroupname());
			stmt.setInt(2, group.getPrgroupid());
			stmt.setString(3, group.getGroupnote());
			stmt.setInt(4, group.getGroupid());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
 	}
     
	/**
	 * ��ѯ���з�����Ϣ
	 * @param group
	 * @param allquery
	 * @return
	 */
	public ArrayList<Groups> query(Groups group,boolean allquery){
		Connection conn = null;
		try {
			conn = DBConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ArrayList<Groups> groups=new ArrayList<Groups>();
		ResultSet rs=null;
		PreparedStatement stmt = null;
		try {
		if(allquery){
			//��ѯ����
			stmt=conn.prepareStatement("select * from groups");
	
			rs=stmt.executeQuery();		  
	   }else{
		//������ѯ
		
	    }
		while(rs.next()){
			Groups g=new Groups();
			g.setGroupid(rs.getInt("groupid"));
			g.setGroupname(rs.getString("groupname"));
			g.setPrgroupid(rs.getInt("prgroupid"));
			g.setGroupnote(rs.getString("groupnote"));
			g.setUnitid(rs.getInt("unitid"));
			g.setUserid(rs.getInt("userid"));
			g.setCrtdate(rs.getDate("crtdate"));
			//g.setCrtusr(rs.getInt("crtusr"));
			//g.setCrtcmp(rs.getInt("crtcmp"));
			g.setUptime(rs.getDate("uptime"));
			//g.setUpusr(rs.getInt("upusr"));
			//g.setUpcmp(rs.getInt("upcmp"));
			groups.add(g);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}/*finally{
			
		}*/
		return groups;
	}
	/**@author Administrator
	 * ͨ��id���Ψһ�������ڸ��²�ѯһ����¼
	 */
	
	public Groups queryByID(int groupid){
		Connection conn = null;
		try {
			conn = DBConnection.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ResultSet rs=null;
		PreparedStatement stmt=null;
		Groups group=new Groups();
		
		try {
			stmt=conn.prepareStatement("select *  from groups where groupid=?");
			stmt.setInt(1, groupid);
			rs=stmt.executeQuery();
			if(rs.next()){
				group.setGroupid(rs.getInt("groupid"));
				group.setGroupname(rs.getString("groupname"));
				group.setPrgroupid(rs.getInt("prgroupid"));
				group.setGroupnote(rs.getString("groupnote"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return group;
	}
	
	/**
	 * ͳ�������� ����ҳ��
	 */
	//ʵ�ַ�ҳ
	private void countPage(Groups group,int items,boolean allquery){
		int count=0;//������
		int pages=0;//��ҳ��
		Connection conn = null;
			try {
				conn = DBConnection.getConnection();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		
		ResultSet rs=null;
		PreparedStatement stmt=null;
		try {
			if(allquery){
			stmt=conn.prepareStatement("select * from groups");
			rs=stmt.executeQuery();
			}else{
				
			}//else
			
			while(rs.next()){	
				count++;	
			
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resultItems=count;
		if(count%items==0)
			pages=count/items;
		else
			pages=count/items+1;
		pageCounts=pages;
	}
	/**
	 * ������ҳ��
	 * @return
	 */
	public int getPageCounts(){		
		return pageCounts;
	}
	
	/**
	 * ���ز鵽��������
	 * @return
	 */
	public int getResultItems(){
		return resultItems;
	}

}


